package ATBMS.modles.DAO;

import ATBMS.modles.Flight;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Flight DAO Implements
 */
public class FlightDAOImpl implements FlightDAO {

    private Connection conn;

    private FlightDAOImpl(Connection conn) {
        this.conn = conn;
    }

    public static FlightDAOImpl getInstance(Connection conn) {
        return new FlightDAOImpl(conn);
    }

    public void delete(int fid) {
        try {
            PreparedStatement ps = conn.prepareStatement("DELETE FROM atbms_flight WHERE fid=?");
            ps.setInt(1, fid);
            ps.execute();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Flight> findAll() {
        List<Flight> ret = new ArrayList<Flight>();
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT fid,fcode,fdest,fairline," +
                    "fgooff,farrival,fbusload,fequip,fprice FROM atbms_flight");
            while (rs.next()) {
                ret.add(new Flight(rs.getInt(1), rs.getString(2), rs.getString(3),
                        rs.getString(4), rs.getTimestamp(5), rs.getTimestamp(6), rs.getInt(7), rs.getString(8), rs.getFloat(9)));
            }
            rs.close();
            stmt.close();
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public Flight findByFlightCode(String code) {
        Flight ret = null;
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT fid,fcode,fdest,fairline," +
                    "fgooff,farrival,fbusload,fequip,fprice FROM atbms_flight WHERE fcode='" + code + "'");
            if (rs.next()) {
                ret = new Flight(rs.getInt(1), rs.getString(2), rs.getString(3),
                        rs.getString(4), rs.getTimestamp(5), rs.getTimestamp(6), rs.getInt(7), rs.getString(8), rs.getFloat(9));


            }
            rs.close();
            stmt.close();
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public boolean insert(Flight flight) {
        try {
            PreparedStatement ps = conn.prepareStatement("INSERT INTO atbms_flight SET " +
                    "fcode=?," +
                    "fdest=?," +
                    "fairline=?," +
                    "fgooff=?," +
                    "farrival=?," +
                    "fbusload=?," +
                    "fequip=?," +
                    "fprice=?");

            ps.setString(1, flight.getFcode());
            ps.setString(2, flight.getFdest());
            ps.setString(3, flight.getFairline());
            ps.setTimestamp(4, new java.sql.Timestamp(flight.getFgooff().getTime()));
            ps.setTimestamp(5, new java.sql.Timestamp(flight.getFarrival().getTime()));
            ps.setInt(6, flight.getFbusload());
            ps.setString(7, flight.getFequip());
            ps.setFloat(8, flight.getFprice());
            ps.execute();
            SQLWarning sqlError = ps.getWarnings();
            boolean ret = (sqlError == null);
            ps.close();
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean update(int fid, Flight flight) {
        try {
            PreparedStatement ps = conn.prepareStatement("UPDATE atbms_flight SET " +
                    "fcode=?," +
                    "fdest=?," +
                    "fairline=?," +
                    "fgooff=?," +
                    "farrival=?," +
                    "fbusload=?," +
                    "fequip=?," +
                    "fprice=? WHERE fid=?");

            ps.setString(1, flight.getFcode());
            ps.setString(2, flight.getFdest());
            ps.setString(3, flight.getFairline());
            ps.setTimestamp(4, new java.sql.Timestamp(flight.getFgooff().getTime()));
            ps.setTimestamp(5, new java.sql.Timestamp(flight.getFarrival().getTime()));
            ps.setInt(6, flight.getFbusload());
            ps.setString(7, flight.getFequip());
            ps.setFloat(8, flight.getFprice());
            ps.setInt(9, fid);

            ps.execute();
            SQLWarning sqlError = ps.getWarnings();
            boolean ret = (sqlError == null);
            ps.close();
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
